<?php
/**
 * Created by PhpStorm.
 * User: reshared
 * Date: 2017/9/29
 * Time: 上午10:32
 */

namespace Index\Model;


class SmsCodeModel extends BaseModel
{
//sms_id 短信id
//mobile 验证码接收的手机号
//code 验证码
//send_time 发送时间
//code_type 验证码类型 0注册验证码1忘记密码验证码2邮箱验证码
//sms_expire_time 有效期截止时间
//邮箱无法注册,故邮箱验证码肯定是忘记密码的验证码
    protected $pk = 'sms_id';

    protected $tableName = 'sms_code';

    const REGISTER = 0;

    const FORGET_MOBILE = 1;

    const FORGET_EMAIL = 2;

    /**
     * 比对
     * @param $code
     * @param $mobile
     * @param $type
     * @return bool
     */
    public function validate($code, $mobile, $type)
    {
        if ($type != self::REGISTER && $type != self::FORGET_EMAIL && $type != self::FORGET_MOBILE) {
            return false;
        }
        //这样查询出来最新的记录
        $lastRecord = $this->where(array('mobile'=>$mobile, 'code_type'=>$type))->find();
        //最新记录是否过期
        if ($lastRecord['sms_expire_time'] < time()) {
            return false;
        }
        //比对正确性
        return $code == $lastRecord['code'];
    }
}